/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sinochem.yunlian.ship.common.util;

import com.sinochem.yunlian.ship.common.constant.RedisKey;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.DoubleSummaryStatistics;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * @author panyanan
 * @version 2017-11-22
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	/** 日期 */
	public final static String DEFAILT_DATE_PATTERN = "yyyy-MM-dd";
	public final static String DEFAILT_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	private static String[] parsePatterns = {
		"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
		"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
		"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	/**
	 * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}
	
	/**
	 * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String formatDateTime(Date date) {
		if(date == null){
			return "";
		}
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前时间字符串 格式（HH:mm:ss）
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前年份字符串 格式（yyyy）
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式（MM）
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式（dd）
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}
	
	/**
	 * 得到当前几点字符串 格式（dd）
	 */
	public static String getHour() {
		return formatDate(new Date(), new Object[]{"HH"});
	}
	
	/**
	 * 得到当前星期字符串 格式（E）星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}

	public static Date parseDate(String str, String parsePatterns) {
		try {
			return parseDate(str, new String[]{parsePatterns});
		} catch ( ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

    /**
     * 获取当前时间前一天
     * @param date
     * @return
     */
	public static Date getBeforeDay(Date date) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -1);  
        date = calendar.getTime();  
        return date;  
    }

	/**
	 * 获取当前时间24小时之后
	 * @param date
	 * @return
	 */
	public static Date getAfterHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR_OF_DAY, +24);
		date = calendar.getTime();
		return date;
	}
	
    /**
     * 获取当前时间前后几天
     * @param date
     * @return
     */
	public static Date getAroundDay(Date date,int offset) {  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, offset);  
        date = calendar.getTime();  
        return date;  
    }

	/**
	 * 判断两个日期小时数，并且转换为指定格式
	 * */
	public static String hourBetween(Date start,Date end) {

//		Date end = DateUtils.addDays(endDate,1);
		Date current =  new Date();
		if (current.after(end)) {
			return "已过期";
		}
		long millSeconds = end.getTime() - current.getTime() ;
		long hours = millSeconds/1000/60/60;
		long day = hours / 24;
		if(day > 1) {
			long hour = hours % 24;
			return day + "天" + hour + "小时";
		}else if (hours > 1 && hours < 24) {
			return  hours + "小时";
		}
		return "小于1小时";
	}

	/**
	 * 判断两个日期小时数，并且转换为指定格式
	 * */
	public static String caculateLeftTime(Date expireTime) {
		if (expireTime == null) {
			return null;
		}
		Date current =  new Date();
		if (current.after(expireTime)) {
			return "已过期";
		}
		long millSeconds = expireTime.getTime() - current.getTime() ;
		long hours = millSeconds/1000/60/60;
		long day = hours / 24;
		if(day >= 1) {
			long hour = hours % 24;
			if(hour != 0) {
				return day + "天" + hour + "小时";
			}else {
				return day + "天";
			}
		}else if (hours >= 1 && hours < 24) {
			return  hours + "小时";
		}
		return "小于1小时";

	}
	
	/**
	 * 
	 * @Description: 当前时间是否是工作日 (周一到周五是工作日)
	 */
	public static boolean isWorkeday(Date date) {  
	       
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		int week = cal.get(Calendar.DAY_OF_WEEK); 
		
		if (week == 7 || week == 1) {
			return false;
		}
		
		return true;
	} 
	
	/**
     * 获取修改后的时间
     * @param date
     * @return
     */
	public static Date getDayByAdd(Date date,int field, int amount) {
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(field, amount);  
        return calendar.getTime();  
    }

	/**
	 * 得到指定年份字符串 格式（yyyy）
	 */
	public static String getYear(Date date) {
		return formatDate(date, "yyyy");
	}



	public static String getShipYear(Date shipYear){
		Date startDate = shipYear;
		Date endDate = new Date();
		Calendar startCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();

		startCalendar.setTime(startDate);
		endCalendar.setTime(endDate);

		int day = endCalendar.get(Calendar.DAY_OF_MONTH)
				- startCalendar.get(Calendar.DAY_OF_MONTH);
		int month = endCalendar.get(Calendar.MONTH)
				- startCalendar.get(Calendar.MONTH);
		int year = endCalendar.get(Calendar.YEAR)
				- startCalendar.get(Calendar.YEAR);
		if (day < 0) {
			month--;
		}
		if (month < 0) {
			month += 12;
			year--;
		}
		if(year>0){
			return year+"年";
		}else if(month>0){
			return  (month*30+day)+"天";
		}else{
			return day+"天";
		}
	}

	/**
	 * 小时---》几天几小时
	 * @param
	 * @return
	 */
	public static String  getDayHour(String  processTime) {

		if("0.0".equals(processTime)){
			return  processTime;
		}
		Double pt = Double.parseDouble(processTime);
		pt = pt*3600;
		int m= pt.intValue();
		int h=m/3600;
		int day = h / 24;
		int hour = h % 24;
		if(day>0){
			return day+"天"+hour+"小时";
		}else{
			return hour+"小时";
		}
	}

	/**
	 * 米---》海里
	 * @param distance
	 * @return
	 */
	public static Double getDistance(String distance){
		Double d = Double.parseDouble(distance);
		if(d!=0){
			d = d/1000/1.852;
			return d;
		}else{
			return new Double(0);
		}

	}
	public static void main(String[] args) throws ParseException {
//		String shipYear = "2018-05-01 00:00:00";
//		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		Date sy = format.parse(shipYear);
//		String str = DateUtils.getShipYear(sy);
//		System.out.println(str);
//		System.out.println(format.format(getDayByAdd(new Date(), Calendar.HOUR, 24)));
		String key = String.format(RedisKey.KEY_DICT_ITEM_LIST_BY_GROUP_PREFIX,"InsuranceType","1");

		System.out.println(key);
	}


}
